package com.io.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.io.dto.StudentInsReq;
import com.io.dto.StudentListPage;
import com.io.entity.one2many.Student;
import com.io.service.TbStudentService;
import com.io.vo.ApiResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * The class/interface
 *
 * @author guodd
 * @version 1.0 use jdk 1.8
 */
@Api(tags = "学生信息")
@RestController
@RequestMapping(value = "/student")
public class StudentController {
    @Autowired
    private TbStudentService tbStudentService;

    @ApiOperation(value = "保存学生信息")
    @PostMapping(value = "doSave")
    public ApiResult doSave(@RequestBody @Validated StudentInsReq studentInsReq) {
        Student tbStudent = new Student();
        BeanUtils.copyProperties(studentInsReq, tbStudent);
        int i = tbStudentService.insertSelective(tbStudent);
        return ApiResult.ok(tbStudent.getSid());
    }

    @PostMapping(value = "getListPage")
    public ApiResult getListPage(@RequestBody StudentListPage studentListPage) {
        PageHelper.startPage(studentListPage.getPageNum(), studentListPage.getPageSize());
        List<Student> byAll = tbStudentService.findByAll(null);
        PageInfo<Student> pageInfo = new PageInfo<>(byAll);
        return ApiResult.ok(pageInfo.getList()).put("total", pageInfo.getTotal());
    }
}
